#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/17 11:08
# @Author  : AshJo
# @Desc    : 日志模块
# @Filename: logging_demo.py.py
# @Version : v1.0
"""

# 写log: 1.排错 2.数据分析 3.操作审计

import time
import logging
from logging import handlers

# 输出日志信息并记录到文件
fh = logging.FileHandler('tmp.log', encoding='utf-8')
sh = logging.StreamHandler()

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s[line:%(lineno)d]-%(module)s: %(message)s]',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    level=logging.DEBUG,
    handlers=[fh, sh]
)

logging.debug('This is debug message!')
logging.info('This is info message!')
logging.warning('This is warning message!')
logging.error('This is error message!')
logging.critical('This is critical message!')


'''日志切分'''

# 输出到终端
sh = logging.StreamHandler()
# 按照大小做切割
rh = handlers.RotatingFileHandler('myapp.log', maxBytes=1024, backupCount=5)
# 按照时间做切割
fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5, encoding='utf-8')

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s[line:%(lineno)d]-%(module)s: %(message)s]',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    level=logging.DEBUG,
    handlers=[sh, rh, fh]
)

# 模拟批量输出1000条日志信息
for i in range(1, 1000):
    time.sleep(1)
    logging.error('KeyboardInterrupt error %s' % str(i))
